package com.ynet.middleground.riskmanage.model.qichacha;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ynet.core.common.BaseReqObjPager;
import com.ynet.core.common.PagerInfo;
import com.ynet.core.exception.BusinessException;
import com.ynet.middleground.riskmanage.bean.qichacha.personcorrelation.*;
import com.ynet.middleground.riskmanage.dao.*;
import com.ynet.middleground.riskmanage.dto.JudicialAssistanceDTO;
import com.ynet.middleground.riskmanage.dto.qichacha.operationstate.ADSTLicenseDetailDTO;
import com.ynet.middleground.riskmanage.dto.qichacha.personcorrelation.*;
import com.ynet.middleground.riskmanage.entity.QichachaAdministrativeLicenseDetail;
import com.ynet.middleground.riskmanage.entity.QichachaPersonHolding;
import com.ynet.middleground.riskmanage.entity.QichachaPersonHoldingPath;
import com.ynet.middleground.riskmanage.external.qcc.ApiHelper;
import com.ynet.middleground.riskmanage.external.qcc.QccUriEnum;
import com.ynet.middleground.riskmanage.utils.BeanUtil;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import sun.security.tools.PathList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PersonCorrelationModel
 * @Description 企查查个人相关接口服务处理类
 * @Author zhangyongbin
 * @Date 2020/8/7 17:50
 * @Version 1.0
 */
@Component
public class PersonCorrelationModel {

    @Autowired
    private ApiHelper apiHelper;

    @Autowired
    private Mapper mapper;

    /**
     * @return com.ynet.middleground.riskmanage.dto.qichacha.personcorrelation.PersonHoldingInfoDTO
     * @Description 企查查--个人控股查询
     * @Date 10:46 2020/8/10
     * @Param req：个人控股查询请求类
     **/
    @Transactional(rollbackFor = Exception.class)
    public PersonHoldingInfoDTO queryPersonHoldingInfo(PersonHoldingCompanyReq req) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("searchKey", req.getSearchKey());
        params.put("personName", req.getPersonName());
        setReqPagingInfo(params, req);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_PERSON_HOLDING_COMPANY_LIST, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);

        JSONObject result = apiResult.getJSONObject("Result").getJSONObject("Data");

//        JSONArray companyNameList = result.getJSONArray("CompanyNameList");

        PersonHoldingInfoDTO personHoldingInfoList = result.toJavaObject(PersonHoldingInfoDTO.class);
//        List<PersonHoldingInfoDTO> personHoldingInfoList = new ArrayList<>();
//
//        PersonHoldingInfoDTO personHoldingInfoDTO = null;
//
//        PersonHoldingOperDTO personHoldingOperDTO = null;
//
//        List<List<PersonHoldingPathDTO>> personHoldingPathParentList = new ArrayList<>();
//
//        List<PersonHoldingPathDTO> personHoldingPathList = null;
//
//        JSONObject jsonObject = null;

//        for (int i = 0; i < companyNameList.size(); i++) {
//            jsonObject = companyNameList.getJSONObject(i);
//            personHoldingInfoDTO = jsonObject.toJavaObject(PersonHoldingInfoDTO.class);
//
//            JSONObject oper = jsonObject.getJSONObject("Oper");
//
//            personHoldingOperDTO = oper.toJavaObject(PersonHoldingOperDTO.class);
//
//            personHoldingInfoDTO.setPersonHoldingOper(personHoldingOperDTO);
//
//            JSONArray pathList = jsonObject.getJSONArray("PathList");
//
//            for (int j=0 ;j<pathList.size();j++){
//
//                personHoldingPathList = pathList.getJSONArray(j).toJavaList(PersonHoldingPathDTO.class);
//                personHoldingPathParentList.add(personHoldingPathList);
//
//            }
//
//            personHoldingInfoDTO.setCompanyList().setPersonHoldingPathList(personHoldingPathParentList);
//
//            personHoldingInfoList.add(personHoldingInfoDTO);
//
//        }
        return personHoldingInfoList;
    }

    /**
     * @Description 个人自身风险条目数
     * @Date 13:54 2020/8/12
     * @Param [req] 个人自身风险条目数请求信息
     * @return com.ynet.middleground.riskmanage.dto.qichacha.personcorrelation.PersonRiskCountDTO
     **/
    public PersonRiskCountDTO getPersonalRiskCountInfo(PersonRiskCountReq req){
        Map<String, Object> params = new HashMap<>(2);
        params.put("searchKey", req.getSearchKey());
        params.put("personName", req.getPersonName());
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_PERSONAL_RISK_COUNT_INFO, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONObject result = apiResult.getJSONObject("Result");

        PersonRiskCountDTO personRiskCountDTO = result.toJavaObject(PersonRiskCountDTO.class);

        return personRiskCountDTO;
    }


    /**
     * @Description  个人自身风险扫描
     * @Date 14:32 2020/8/12
     * @Param [req] 个人自身风险扫描请求信息
     * @return com.ynet.middleground.riskmanage.dto.qichacha.personcorrelation.PersonRiskCheckInfoDTO
     **/
    public PersonRiskCheckInfoDTO getPersonRiskCountCheckInfo(PersonRiskCountInfoReq req){
        Map<String, Object> params = new HashMap<>(2);
        params.put("searchKey", req.getSearchKey());
        params.put("personName", req.getPersonName());
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_PERSON_RISK_COUNT_CHECK_INFO, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }

        PersonRiskCheckInfoDTO personRiskCheckInfoDTO = new PersonRiskCheckInfoDTO();

        PersonRiskCheckDataDTO personRiskCheckDataDTO = null;

        JSONObject result = apiResult.getJSONObject("Result");

        Integer verifyResult = result.getInteger("VerifyResult");

        if(verifyResult == 1){
            JSONObject data = result.getJSONObject("Data");

            Map<String,String> map  = new HashMap(14);
            //进行key值转换
            map.put("SXCount","sxCount");
            map.put("ZXCount","zxCount");
            map.put("STCount","stCount");
            map.put("ENCCount","encCount");
            map.put("JUDCount","judCount");
            map.put("CTACount","ctaCount");
            map.put("UNLCount","unlCount");
            map.put("STFCount","stfCount");
            map.put("STPCount","stpCount");
            map.put("PLECount","pleCount");
            map.put("LACount","laCount");
            map.put("CSACount","csaCount");
            map.put("DLNCount","dlnCount");
            map.put("INECount","ineCount");
            data =   BeanUtil.changeJsonObj(data,map);
            personRiskCheckDataDTO = data.toJavaObject(PersonRiskCheckDataDTO.class);

        }

        personRiskCheckInfoDTO.setVerifyResult(verifyResult);
        personRiskCheckInfoDTO.setData(personRiskCheckDataDTO);

        return personRiskCheckInfoDTO;
    }

    /**
     * @Description 个人自身风险-失信被执行核查
     * @Author zhangyongbin
     * @Date 2020/8/19 10:53
     * @Param [req]
     * @return com.ynet.middleground.riskmanage.dto.qichacha.personcorrelation.PersonSXCheckInfoDTO
     **/
    public PersonSXCheckInfoDTO getPersonSXCheckList(PersonRiskInfoReq req){
        Map<String, Object> params = new HashMap<>(2);
        params.put("searchKey", req.getSearchKey());
        params.put("personName", req.getPersonName());
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_PERSONAL_SXRISK_INFO, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }

        PersonSXCheckInfoDTO personSXCheckInfoDTO = new PersonSXCheckInfoDTO();

        List<PersonSXCheckDataDTO> personSXCheckDataDTOList = null;

        JSONObject result = apiResult.getJSONObject("Result");

        Integer verifyResult = result.getInteger("VerifyResult");

        if(verifyResult == 1){
            JSONArray data = result.getJSONArray("Data");
            personSXCheckDataDTOList = data.toJavaList(PersonSXCheckDataDTO.class);

        }

        personSXCheckInfoDTO.setVerifyResult(verifyResult);
        personSXCheckInfoDTO.setData(personSXCheckDataDTOList);

        return personSXCheckInfoDTO;
    }
    
    /**
     * @Description 个人自身风险-被执行人核查 
     * @Author zhangyongbin
     * @Date 2020/8/19 10:56
     * @Param [req]
     * @return com.ynet.middleground.riskmanage.dto.qichacha.personcorrelation.PersonZXCheckInfoDTO
     **/
    public PersonZXCheckInfoDTO getPersonZXCheckList(PersonRiskInfoReq req){
        Map<String, Object> params = new HashMap<>(2);
        params.put("searchKey", req.getSearchKey());
        params.put("personName", req.getPersonName());
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_PERSONAL_ZXRISK_INFO, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }

        PersonZXCheckInfoDTO personZXCheckInfoDTO = new PersonZXCheckInfoDTO();

        List<PersonZXCheckDataDTO> personZXCheckDataDTOList = null;

        JSONObject result = apiResult.getJSONObject("Result");

        Integer verifyResult = result.getInteger("VerifyResult");

        if(verifyResult == 1){
            JSONArray data = result.getJSONArray("Data");
            personZXCheckDataDTOList = data.toJavaList(PersonZXCheckDataDTO.class);

        }

        personZXCheckInfoDTO.setVerifyResult(verifyResult);
        personZXCheckInfoDTO.setData(personZXCheckDataDTOList);

        return personZXCheckInfoDTO;
    }

    /**
     * @Description 个人自身风险-限制高消费核查
     * @Author zhangyongbin
     * @Date 2020/8/19 10:57
     * @Param [req]
     * @return com.ynet.middleground.riskmanage.dto.qichacha.personcorrelation.PersonSumptuaryCheckInfoDTO
     **/
    public PersonSumptuaryCheckInfoDTO getPersonSumptuaryCheckList(PersonRiskInfoReq req){
        Map<String, Object> params = new HashMap<>(2);
        params.put("searchKey", req.getSearchKey());
        params.put("personName", req.getPersonName());
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_PERSONAL_SUMPTUARY_RISK_INFO, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }

        PersonSumptuaryCheckInfoDTO personSumptuaryCheckInfoDTO = new PersonSumptuaryCheckInfoDTO();

        List<PersonSumptuaryCheckDataDTO> personSumptuaryCheckDataDTOList = null;

        JSONObject result = apiResult.getJSONObject("Result");

        Integer verifyResult = result.getInteger("VerifyResult");

        if(verifyResult == 1){
            JSONArray data = result.getJSONArray("Data");
            personSumptuaryCheckDataDTOList = data.toJavaList(PersonSumptuaryCheckDataDTO.class);

        }

        personSumptuaryCheckInfoDTO.setVerifyResult(verifyResult);
        personSumptuaryCheckInfoDTO.setData(personSumptuaryCheckDataDTOList);

        return personSumptuaryCheckInfoDTO;
    }

    /**
     * 设置企查查分页请求信息参数
     *
     * @param params   请求参数集合
     * @param reqPager 当前分页请求类
     */
    protected void setReqPagingInfo(Map<String, Object> params, BaseReqObjPager reqPager) {
        params.put("pageIndex", reqPager.getPagerInfo().getPageNum());
        params.put("pageSize", reqPager.getPagerInfo().getPageSize());
    }

    /**
     * 如果 api 结果返回分页信息，则将分页结果重新添加到请求分页对象中
     *
     * @param apiResult 企查查 api 结果
     * @param reqPager 请求分页信息
     */
    protected void setPageInfo(JSONObject apiResult, BaseReqObjPager reqPager) {
        JSONObject paging = apiResult.getJSONObject("Paging");
        if (paging == null) {
            return;
        }
        PagerInfo pagerInfo = reqPager.getPagerInfo();
        pagerInfo.setTotal(paging.getInteger("TotalRecords"));
    }

    /**
     * @Description 人员相关-个人失信信息核查
     * @Author lwq
     * @Date 2020/11/16 14:39
     * @Param [req]人员相关-个人失信信息核查请求类
     * @return com.ynet.core.common.ServiceResult<com.ynet.middleground.riskmanage.dto.qichacha.personcorrelation.PersonDishonestInfoDTO>
     **/
//    public PersonDishonestInfoDTO getPersonDishonestInfoList(PersonDishonestInfoReq req){
//        Map<String, Object> params = new HashMap<>(2);
//        params.put("personName", req.getPersonName());
//        params.put("idnum", req.getIdnuml());
//        setReqPagingInfo(params, req);
//        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_DISHONEST_INFO, params, req);
//        Integer code = apiResult.getInteger("Status");
//        String message = apiResult.getString("Message");
//        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
//            throw new BusinessException(message, String.valueOf(code));
//        }
//        PersonDishonestInfoDTO resp = new PersonDishonestInfoDTO();
//        List<PersonDishonestDataDTO> personDishonestDataDTOList = null;
//        JSONObject result = apiResult.getJSONObject("Result");
//        Integer verifyResult = result.getInteger("VerifyResult");
//        if(verifyResult == 1){
//            JSONArray data = result.getJSONArray("Data");
//            personDishonestDataDTOList = data.toJavaList(PersonDishonestDataDTO.class);
//        }
//
//        //返回的分页信息
//        setPageInfo(apiResult, req);
//
//        resp.setVerifyResult(verifyResult);
//        resp.setData(personDishonestDataDTOList);
//        return resp;
//    }

    /**
     * @Description 人员相关-个人被执行人核查
     * @Author lwq
     * @Date 2020/11/16 14:39
     * @Param [req]人员相关-个人被执行人核查请求类
     * @return com.ynet.core.common.ServiceResult<com.ynet.middleground.riskmanage.dto.qichacha.personcorrelation.PersonExecutedInfoDTO>
     **/
    public PersonExecutedInfoDTO getPersonExecutedInfoList(PersonExecutedInfoReq req){
        Map<String, Object> params = new HashMap<>(2);
        params.put("personName", req.getPersonName());
        params.put("idnum", req.getIdnuml());
        setReqPagingInfo(params, req);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_EXECUTED_INFO, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        PersonExecutedInfoDTO resp = new PersonExecutedInfoDTO();
        List<PersonExexutedDataDTO> personExexutedDataDTOList = null;
        JSONObject result = apiResult.getJSONObject("Result");
        Integer verifyResult = result.getInteger("VerifyResult");
        if(verifyResult == 1){
            JSONArray data = result.getJSONArray("Data");
            personExexutedDataDTOList = data.toJavaList(PersonExexutedDataDTO.class);
        }

        //返回的分页信息
        setPageInfo(apiResult, req);

        resp.setVerifyResult(verifyResult);
        resp.setData(personExexutedDataDTOList);
        return resp;
    }

}
